package org.strieber.fca;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class TFMGUI extends JApplet {

	private TabbedFileManipulator tfm;
	
	private JMenuBar menuBar = new JMenuBar();
	private JMenu jmFile = new JMenu("File");
	private JMenu jmHelp = new JMenu("Help");
	private JMenuItem jmiNew = new JMenuItem("New");
	private JMenuItem jmiOpen = new JMenuItem("Open");
	private JMenuItem jmiExport = new JMenuItem("Export");
	private JMenuItem jmiExit = new JMenuItem("Exit");
	private JMenuItem jmiAbout = new JMenuItem("About");
	
	private JTable jtblKeep = new JTable();
	private JList jlstDelete = new JList();
	
	private JScrollPane jspKeep = new JScrollPane(jtblKeep);
	private JScrollPane jspDelete = new JScrollPane(jlstDelete);
	
	private JPanel jpnlButtons = new JPanel();
	private JButton jbtDelete = new JButton("Delete");
	private JButton jbtRestore = new JButton("Restore");
	private JButton jbtCommit = new JButton("Commit");
	
	public TFMGUI() {
		// place menu bar
		menuBar.add(jmFile);
		menuBar.add(jmHelp);
		jmFile.add(jmiNew);
		jmFile.add(jmiOpen);
		jmFile.add(jmiExport);
		jmFile.add(jmiExit);
		jmHelp.add(jmiAbout);
		setJMenuBar(menuBar);
		// set up rest of GUI
		setLayout(new BorderLayout());
		jpnlButtons.setLayout(new GridLayout(3,1));
		jpnlButtons.add(jbtDelete);
		jpnlButtons.add(jbtRestore);
		jpnlButtons.add(jbtCommit);
		add(jpnlButtons, BorderLayout.SOUTH);
		add(jspKeep, BorderLayout.WEST);
		add(jspDelete, BorderLayout.EAST);
		// add action listeners
		jmiNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
			
			}
		});
		jmiOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				JFileChooser jfcOpen = new JFileChooser();
				FileFilter filter = new FileNameExtensionFilter("Tabbed Files", "txt");
				jfcOpen.addChoosableFileFilter(filter);
				
				int jfcReturn = jfcOpen.showDialog(null, "Open File");
				
				if (jfcReturn == JFileChooser.APPROVE_OPTION) {
					File file = jfcOpen.getSelectedFile();
					try {
						tfm = new TabbedFileManipulator(file);
						// populate table from tfm
					}
					catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		jmiExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				JFileChooser jfcExport = new JFileChooser();
				FileFilter filter = new FileNameExtensionFilter("Tabbed Files", "txt");
				jfcExport.addChoosableFileFilter(filter);
				
				int jfcReturn = jfcExport.showDialog(null, "Export File");
				
				if (jfcReturn == JFileChooser.APPROVE_OPTION) {
					File file = jfcExport.getSelectedFile();
					try {
						tfm.exportTabbedFile(file);
						// populate table from tfm
					}
					catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		jmiExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				System.exit(0);
			}
		});
		jmiAbout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
			
			}
		});
		jbtDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
			
			}
		});
		jbtRestore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
			
			}
		});
		jbtCommit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
			
			}
		});
	}
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("TabbedFileManipulator");
		JApplet TFMGUI = new TFMGUI();
		frame.add(TFMGUI);
		frame.setSize(700, 400);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
}
